package com.zhaipuhong.j2ee.jmail;

import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
 * @author JohnLan
 * @E-mail:JohnMr.Lan@foxmail.com
 * @qq:757553326
 * @version 创建时间：2012-10-10 下午04:02:12 简单说明
 */

public class ControlJMail {

	/**
	 * 发送邮件
	 * 
	 * @param from
	 *            邮件发送人的邮件地址
	 * @param to
	 *            邮件接收人的邮件地址
	 * @param username
	 *            发件人的邮件帐户
	 * @param password
	 *            发件人的邮件密码
	 * @param object
	 *            邮件标题
	 * @param content
	 *            邮件内容
	 */
	public static void sendMail(String from, String to, final String username,
			final String password, String object, String content) {
		String host = "localhost";
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					@Override
					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}

				});

		try {
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			message.setSubject(object);
			message.setText(content);
			session.getTransport("smtp").send(message);
			System.out.println("发送邮件完成!");
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 接收邮件
	 * 
	 * @param username
	 *            接收邮件帐号
	 * @param password
	 *            接收邮件密码
	 * @return
	 */
	public static String getMail(final String username, final String password) {
		String host = "localhost";
		String resultContent = "";
		Properties props = new Properties();
		// 创建邮件会话
		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					@Override
					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}

				});

		try {
			// 获取邮箱的pop3存储
			Store store = session.getStore("pop3");
			store.connect(host, username, password);
			// 获取inbox文件
			Folder folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY); // 打开，打开后才能读取邮件信息
			// 获取邮件消息
			Message message[] = folder.getMessages();

			for (int i = 0, n = message.length; i < n; i++) {
				System.out.println(i + ": " + message[i].getFrom()[0] + "\t"
						+ message[i].getSubject());
				try {
					resultContent = resultContent + message[i];
					message[i].writeTo(System.out);
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

			// 关闭资源
			folder.close(false);
			store.close();

		} catch (MessagingException e) {
			e.printStackTrace();
		}

		System.out.println("收取邮件完成!");
		return resultContent;
	}

	/**
	 * 邮件主题中文字符转换
	 */
	public static String transferChinese(String strText) {
		try {
			strText = MimeUtility.encodeText(new String(strText.getBytes(),
					"GB2312"), "GB2312", "B");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strText;
	}

}